import json
from threading import Thread

import requests

from PyQt5.QtCore import QThread, pyqtSignal


class QtThread(QThread):
    load_data = pyqtSignal(dict)

    def __init__(self, url, data=None, header=None, cookies=None, method='GET'):
        super().__init__()
        self.url = url
        self.cookies = cookies
        self.data = data
        self.header = header
        self.method = method

    def run(self):
        if self.method.upper() == 'GET':
            response = requests.get(url=self.url, data=self.data, headers=self.header, cookies=self.cookies)
        elif self.method.upper() == 'POST':
            response = requests.post(self.url, data=self.data, headers=self.header, cookies=self.cookies)
        elif self.method.upper() == 'PUT':
            response = requests.put(self.url, data=self.data, headers=self.header, cookies=self.cookies)
        elif self.method.upper() == 'DELETE':
            response = requests.delete(self.url, data=self.data, headers=self.header, cookies=self.cookies)
        elif self.method.upper() == 'HEAD':
            response = requests.head(self.url, data=self.data, headers=self.header, cookies=self.cookies)

        data = json.loads(response.text)
        self.load_data.emit(data)
